package servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import utils.DBUtil;

public class RegSverlet extends HttpServlet {
	
	@SuppressWarnings("unused")
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		String username = req.getParameter("username");
		String userphone = req.getParameter("userphone");
		String password = req.getParameter("password");
		String password2 = req.getParameter("password2");
	
	
		
		//验证是否是否有相同的手机号
		Connection conn = DBUtil.getConn();
		try {
			/*String sql="SELECT USERNAME FROM login WHERE USERNAME=?";
			
				PreparedStatement stmt = conn.prepareStatement(sql);
				stmt.setString(1, username);
				ResultSet rs = stmt.executeQuery();
				if(rs.next()){
					System.out.println("手机号码已经注册");
					req.getRequestDispatcher("reg.html").forward(req, resp);
				}else{
					//验证密码是否相同
					*/
					String sql2="INSERT INTO login(USERNAME,PASSWORD) VALUES(?,?)";
					PreparedStatement ps = conn.prepareStatement(sql2);
					ps.setString(1,username);
					ps.setString(2,password);
					int i = ps.executeUpdate();
					
					if(i!=0){
						req.getRequestDispatcher("log.html").forward(req, resp);
					}
				
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		
		
	}

}
